package com.fz.study.project.controller;


import com.alibaba.fastjson.JSON;
import com.fz.study.project.anno.Log;
import com.fz.study.project.entity.Result;
import com.fz.study.project.entity.StudentUser;
import com.fz.study.project.entity.dto.StudentCourseDto;
import com.fz.study.project.entity.dto.StudentUserDto;
import com.fz.study.project.entity.qurey.StudentUserQuery;
import com.fz.study.project.service.IStudentCourseService;
import com.fz.study.project.service.IStudentUserService;
import com.fz.study.project.utils.FileExclUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-02-07
 */
@Tag(name = "学生资料")
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/studentuser")
public class StudentUserController {

    private final IStudentUserService studentUserService;
    private final HttpServletResponse response;
    private final IStudentCourseService studentCourseService;

    @Log
    @Operation(summary = "添加学生信息")
    @PutMapping("add")
    public Result save(@RequestBody StudentUserDto studentUserDto){
        StudentUser studentUser = new StudentUser();
        BeanUtils.copyProperties(studentUserDto,studentUser);
        studentUserService.saveByName(studentUser);
        return Result.success("新增用户成功！");
    }

    @Operation(summary = "显示学生信息")
    @PostMapping("list")
    public Result list(){
        return Result.success(studentUserService.listByName());
    }

    @Log
    @Operation(summary = "修改学生信息")
    @PutMapping("update")
    public Result updateByName(@RequestBody StudentUser studentUser){
        studentUserService.updateByName(studentUser);
        return Result.success("修改成功！");
    }
    @Log
    @Operation(summary = "批量删除学生信息")
    @DeleteMapping("{ids}")
    public Result removeByIds(@PathVariable List<Integer> ids){
        studentUserService.deleteByIds(ids);
        return Result.success("删除成功！");
    }

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result page(@RequestBody StudentUserQuery studentUserQuery){
        return Result.success(studentUserService.findByPage(studentUserQuery));
    }

    @Operation(summary = "导出学生信息到Excel")
    @PutMapping("/file")
    public void putFile() throws Exception {
        List<StudentUser> student = studentUserService.list();
        String jsonString = JSON.toJSONString(student);
        FileExclUtils.exportExcel("学生信息", jsonString,StudentUser.class,response);
    }

    @Operation(summary = "学生选择课程")
    @PostMapping("choose")
    public Result chooseCourse(@RequestBody StudentCourseDto studentCourseDto){

        studentCourseService.chooseCourse(studentCourseDto);

        return Result.success("添加课程成功");

    }
}
